<?php

class Euso_Model_Mail extends Zend_Mail
{
	protected static $_options = NULL;
	protected static $_transport = NULL;

	public function __construct($charset = 'iso-8859-1')
	{
		self::_initTransport();
		parent::__construct($charset);
	}

	protected static function _initTransport()
	{
		if (self::$_transport === NULL) {

			$globalOpts = Kernel::getInstance()->getOptions();
			if (is_array($globalOpts) === FALSE || is_array($globalOpts['mail']) === FALSE) {
				throw new Exception('No mail configuration');
			}

			self::$_options = $globalOpts['mail'];


			$transport = 'sendmail';
			$host = NULL;
			$params = array();

			if (isset(self::$_options['transport'])) {
				if (isset(self::$_options['transport'])) {
					$transport = self::$_options['transport'];
				}
				if (isset(self::$_options['host'])) {
					$host = self::$_options['host'];
				}
				if (is_array(self::$_options['params'])) {
					$params = self::$_options['params'];
				}
			}


			//DEV: email options override (dunno how to setup [fake] sendmail with Zend_Mail on Windows)
			@include "../mailopts.php";

			if ($transport == 'smtp') {
				self::$_transport = new Zend_Mail_Transport_Smtp($host, $params);
			} else {
				self::$_transport = new Zend_Mail_Transport_Sendmail();
			}

			Zend_Mail::setDefaultTransport(self::$_transport);
		}
	}
}
